package com.ksm.zyz.common.util;

import org.apache.commons.lang3.RandomStringUtils;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by ailot on 2015/11/30.
 */
public class TokenUtil {

    /*private MessageDigest digest;

    private String encrypt(String strSrc){
        String strDes = null;
        byte[] bt = strSrc.getBytes();
        digest.update(bt);
        strDes = EncodeUtil.encodeHex(digest.digest());
        return strDes;
    }*/

    /**
     * 根据用户id生成token
     * @param userId
     * @return
     */
    public static String generateToken(String userId){
        StringBuffer sb = new StringBuffer(userId);
        String random = RandomStringUtils.random(3,false,true);
        long time = new Date().getTime();
        sb.append(time).append(random);
        String token = EncodeUtil.encodeBase64(sb.toString());
        return token;
    }
    
    /**
     * 根据token获取用户id
     * @param token
     * @return
     */
    public static String getIdByToken(String token){
    	String code = EncodeUtil.decodeBase64String(token);
    	String userId = "";
    	if (code != null && !code.equals("")) {
    		if (code.length() > 32) {
    			userId = code.substring(0,32);
			}
		}
    	return userId;
    }

    
    /**
     * 验证token
     * @param user_token 用户手机存储token
     * @param local_token 数据库存储token
     * @return
     */
    public static boolean validateToken(String user_token,String local_token) {
    	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	String user_date_toString = "";
    	String local_date_toString = "";
    	//截取用户token中生成时间
        String user_code = EncodeUtil.decodeBase64String(user_token);
        if (!user_code.equals("") && user_code.length() == 48) {
        	String user_time = user_code.substring(32,45);
            Date user_date =new Date(Long.valueOf(user_time));
            user_date_toString = df.format(user_date);
		}else{
			return false;
		}
        //截取数据库中生成时间
        String local_code = EncodeUtil.decodeBase64String(local_token);
    	String local_time = local_code.substring(32,45);
        Date local_date =new Date(Long.valueOf(local_time));
        local_date_toString = df.format(local_date);
        //比较时间是否相等
        if (!user_date_toString.equals("") && !local_date_toString.equals("") && user_date_toString.equals(local_date_toString)) {
			return true;
		}
        return false;
    }
    
    /**
     * 测试main
     * @param args
     */
    public static void main(String[] args) {
    	String user_id = UUIDUtils.random();
    	System.out.println("用户Id："+user_id);
    	String token = generateToken(user_id);
    	System.out.println("用户token："+token);
    	String code = EncodeUtil.decodeBase64String(token);
        String time = code.substring(32,45);
        Date date =new Date(Long.valueOf(time));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String d = df.format(date);
        System.out.println("加密时间："+d);
        
        String user_id1 = UUIDUtils.random();
    	System.out.println("用户Id1："+user_id1);
    	String token1 = generateToken(user_id1);
    	System.out.println("用户token1："+token);
    	String code1 = EncodeUtil.decodeBase64String(token1);
        String time1 = code1.substring(32,45);
        Date date1 =new Date(Long.valueOf(time1));
        String d1 = df.format(date1);
        System.out.println("加密时间1："+d1);
        if (d.equals(d1)) {
			System.out.println("true");
		}else{
			System.out.println("false");
		}
	}
}
